#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 4e4 + 5;

int n, m;
int a[N], b[N], num, pos[N];
int l[205], r[205], sum[205][N], mode[205][205];
int c[N], mxid;
int ans;
void discrete() {
  rep(i, 1, n) b[i] = a[i];
  sort(b + 1, b + n + 1);
  num = unique(b + 1, b + n + 1) - b - 1;
  rep(i, 1, n) a[i] = lower_bound(b + 1, b + num + 1, a[i]) - b;
}
void add(int x, int d) {
  if (!d) return;
  c[x] += d;
  if (c[x] > c[mxid] || (c[x] == c[mxid] && x < mxid)) mxid = x;
}
void init() {
  int k = sqrt(n);
  rep(i, 1, n) pos[i] = (i - 1) / k + 1;
  int cnt = pos[n];
  rep(i, 1, cnt) {
    l[i] = (i - 1) * k + 1;
    r[i] = i * k;
  }
  r[cnt] = n;
  rep(i, 1, cnt) {
    rep(j, 1, num) sum[i][j] = sum[i - 1][j];
    rep(j, l[i], r[i]) sum[i][a[j]]++;
  }
  rep(i, 1, cnt) {
    mxid = 0;
    rep(j, 1, num) c[j] = 0;
    rep(j, i, cnt) {
      rep(k, l[j], r[j]) add(a[k], 1);
      mode[i][j] = mxid;
    }
  }
}
int solve(int L, int R) {
  set<int> cand;
  int id1 = pos[L], id2 = pos[R];
  if (id1 == id2)
    rep(i, L, R) cand.insert(a[i]);
  else {
    rep(i, L, r[id1]) cand.insert(a[i]);
    rep(i, l[id2], R) cand.insert(a[i]);
    if (id1 + 1 <= id2 - 1) cand.insert(mode[id1 + 1][id2 - 1]);
  }
  mxid = 0;
  for (int x : cand) c[x] = 0;
  if (id1 == id2)
    rep(i, L, R) add(a[i], 1);
  else {
    rep(i, L, r[id1]) add(a[i], 1);
    rep(i, l[id2], R) add(a[i], 1);
    if (id1 + 1 <= id2 - 1)
      for (int x : cand) add(x, sum[id2 - 1][x] - sum[id1][x]);
  }
  return b[mxid];
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  rep(i, 1, n) cin >> a[i];
  discrete();
  init();
  while (m--) {
    int x, y;
    cin >> x >> y;
    x = (x + ans - 1) % n + 1;
    y = (y + ans - 1) % n + 1;
    if (x > y) swap(x, y);
    ans = solve(x, y);
    cout << ans << endl;
  }
  return 0;
}